<?php
class Controller_Online_ProductInfo extends Controller_Abstract
{
	function actionDetail()
	{
		$this->_view['title'] = '资料操作';
		if (isset($_GET['number']))
		{
			$number = str_pad($_GET['number'], 6, 0, STR_PAD_LEFT);
			$edit_id = isset($_GET['product_info_id']) ? $_GET['product_info_id'] : 0;
			$info = Product_basic::find('product_basic.number=?', $number)->asArray()->getOne();
			$product_id = isset($info['id']) ? $info['id'] : 0;
			if ($product_id)
			{
				$product = Product_Basic::find('id = ?', $product_id)->asArray()->getAll();
				Helper_BSS_Normal::getProductImgUrl($product);
				$product = $product[0];
				$product_info = Online_Product_Info::find('product_basic_id = ?', $product_id)->asArray()->getAll();
				$lang_val = array();
				$lang = array();
				foreach ($product_info as $pi)
				{
					$lang[] = $pi['lang'];
				}
				foreach (Q::ini('custom_flag/online_ebay_product_language') as $l)
				{
					if (in_array($l['value'], $lang))
					{
						unset($l);
					}
					else
					{
						$lang_val[] = $l;
					}
				}
				$this->_view['language'] = $lang_val;
				$this->_view['product'] = $product;
				$this->_view['product_info'] = $product_info;
			}
			else
			{
				$this->_view['message'] = '不存在产品' . $number;
			}
			if ($edit_id)
			{
				$product_edit = Online_Product_Info::find('product_info.id=? and product_basic_id=?', $edit_id, $product_id)
					->joinleft('product_basic', 'product_basic.number, product_basic.name', 'product_basic.id=product_info.product_basic_id')
					->asArray()->getAll();
				Helper_BSS_Normal::getProductImgUrl($product_edit, 'number');
				$product_edit[0]['form'] = 'edit';
				$this->_view['product_edit'] = $product_edit[0];
			}
		}
	}
	
	function actionCreateSave()
	{
		$this->_view['title'] = '新建(保存)';
		if ($this->_context->isPOST())
		{
			if (empty($_POST['lang']))
			{
				return $this->_redirectMessage('保存失败', '请选择语种', $_SERVER['HTTP_REFERER'], FAILURE);
			}
			else
			{
				if (empty($_POST['title']) || empty($_POST['description']))
				{
					return $this->_redirectMessage('保存失败', '标题或描述不能为空!', $_SERVER['HTTP_REFERER'], FAILURE);
				}
				else
				{
					$result = Online_Product_Info::create($_POST);
					if ($result['ack'] == SUCCESS)
					{
						return $this->_redirectMessage('保存成功', '正在返回..', $_SERVER['HTTP_REFERER'], SUCCESS);
					}
					else
					{
						return $this->_redirectMessage('保存失败', $result['message'], $_SERVER['HTTP_REFERER'], FAILURE);
					}
				}
			}
		}
		else
		{
			return $this->_redirectMessage('提交方式有误', '正在返回...', $_SERVER['HTTP_REFERER'], FAILURE);
		}
	}
	
	function actionEditSave()
	{
		$this->_view['title'] = '编辑(保存)';
		if ($this->_context->isPOST())
		{
			if ($_POST['id'])
			{	
				$result = Online_Product_Info::edit($_POST);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('编辑成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('编辑失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
		}
		else
		{
			return $this->_redirectMessage('提交方式有误', '正在返回...', $_SERVER['HTTP_REFERER'], FAILURE);
		}
	}
	
	function actionSKUList()
	{
		$this->_view['title'] = '统计表';
		$product_categories = Product_Categories::getCategoriesTree();
		$this->_view['product_categories'] = json_encode($product_categories);
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 0;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$this->_view['url_arr'] = json_encode(serialize($url_arr), JSON_UNESCAPED_UNICODE);
		$result = Online_Product_Info::searchDistinct($url_arr, $cp, $ps);
		Helper_BSS_Normal::getProductImgUrl($result['data']);
		if ($result['ack'] == SUCCESS)
		{
			$return = array();
			foreach ($result['data'] as $r)
			{
				$arr = array();
				$product_info = Online_Product_Info::find('product_basic_id=?', $r['id'])
					->setColumns('lang')
					->asArray()->getAll();
				if ($product_info)
				{
					foreach($product_info as $pi)
					{
						$arr[] = $pi['lang'];
					}
				}
				if ( ! array_key_exists($r['number'], $return))
				{		
					$return[$r['number']]['lang'] = $arr;
					$return[$r['number']]['name'] = $r['name'];
					$return[$r['number']]['pic'] = $r['pic_url'];
				}
			}
			$page = new Helper_BSS_Page($result['pagination'], url('Online::ProductInfo/SKUList', $url_arr));
			$this->_view['page'] = $page->getPage();
			$this->_view['product'] = $return;
		}
	}
	
	function actionExport()
	{
		$this->_view['title'] = '导出统计表';
		$result = Online_Product_Info::searchDistinct(unserialize($_GET['search']));
		include Q::ini('custom_system/lib_dir') . 'PHPExcel.php';
		$phpexcel = new PHPExcel();
		$phpexcel->getActiveSheetIndex(0);
		$phpexcel->getActiveSheet()->setTitle('产品资料描述统计表');
		$phpexcel->getActiveSheet()->setCellValue('A1', 'SKU');
		$i = 2;
		foreach ($result['data'] as $r)
		{
			$phpexcel->getActiveSheet()->setCellValueExplicit('A' . $i, $r['number'], PHPExcel_Cell_DataType::TYPE_STRING);
			$i++;
		}
		$outputFileName = '产品资料描述统计表.xls';
		$objWriter = new PHPExcel_Writer_Excel5($phpexcel);
		header("Content-Type: application/vnd.ms-excel");
		header("Content-Type: application/octet-stream");
		header("Content-Type: applicaiton/download");
		header('Content-Disposition:inine;filename="' . $outputFileName . '"');
		header("Content-Transfer-Encoding: binary");
		header("Pragma: no-cache");
		$objWriter->save('php://output');
		exit;
	}
	
	function actionImport()
	{
		$this->_view['title'] = '导入信息';
	}
	
	function actionImportSave()
	{
		$this->_view['title'] = '导入信息（保存）';
		if ($this->_context->isPOST())
		{
			$upload_result = Upload_Files::uploadFile('product_info', Q::ini('custom_flag/upload_file_type/online_product_info/value'));
			if ($upload_result['ack'] == SUCCESS)
			{
				$product_info = Helper_BSS_XLS::xlsFileToArray($upload_result['path'], $upload_result['file_id']);
				$result = Online_Product_info::import($product_info, $_POST['type']);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('导入成功', '正在返回..', url('Online::ProductInfo/Import'), $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('部分导入失败', $result['message'], url('Online::ProductInfo/Import'), $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('文件导入失败', '正在返回..', $_SERVER['HTTP_REFERER'], $upload_result['ack']);
			}
		}
	}
	
	function actionList()
	{
		$this->_view['title'] = '产品资料表';
		$product_categories = Product_Categories::getCategoriesTree();
		$this->_view['product_categories'] = json_encode($product_categories);
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 0;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$this->_view['url_arr'] = json_encode(serialize($url_arr));
		$result = Online_Product_Info::search($url_arr, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			Helper_BSS_Normal::getProductImgUrl($result['data']);
			$page = new Helper_BSS_Page($result['pagination'], url('Online::ProductInfo/List', $url_arr));
			$this->_view['page'] = $page->getPage();
			$this->_view['product_info'] = $result['data'];
		}
	}
	
	function actionExportCheck()
	{
		$this->_view['title'] = '导出（按勾选）';
		$ids = $_POST['ids'];
		$product_info = Online_Product_Info::find('product_info.id in (' . $ids . ')')
			->joinleft('product_basic', 'product_basic.number, product_basic.name', 'product_basic.id=product_info.product_basic_id')
			->asArray()->getAll();
		include Q::ini('custom_system/lib_dir') . 'PHPExcel.php';
		$phpexcel = new PHPExcel();
		$phpexcel->getActiveSheetIndex(0);
		$phpexcel->getActiveSheet()->setTitle('产品资料描述-按勾选');
		$phpexcel->getActiveSheet()->getStyle("A1:D1")->getFont()->setBold(true);
		$phpexcel->getActiveSheet()->setCellValue('A1', 'SKU');
		$phpexcel->getActiveSheet()->setCellValue('B1', 'Language');
		$phpexcel->getActiveSheet()->getColumnDimension('C')->setWidth(120);
		$phpexcel->getActiveSheet()->setCellValue('C1', 'Title');
		$phpexcel->getActiveSheet()->getColumnDimension('D')->setWidth(120);
		$phpexcel->getActiveSheet()->setCellValue('D1', 'Description');
		$i = 2;
		foreach ($product_info as $pi)
		{
			$phpexcel->getActiveSheet()->setCellValueExplicit('A' . $i, $pi['number'], PHPExcel_Cell_DataType::TYPE_STRING);
			$phpexcel->getActiveSheet()->setCellValue('B' . $i, Helper_BSS_Normal::getIniOne('online_ebay_product_language', 'value', $pi['lang'], 'text'));
			$phpexcel->getActiveSheet()->setCellValue('C' . $i, $pi['title']);
			$phpexcel->getActiveSheet()->setCellValue('D' . $i, $pi['description']);
			$i++;
		}
		$outputFileName = '产品资料描述-按勾选.xls';
		$objWriter = new PHPExcel_Writer_Excel5($phpexcel);
		header("Content-Type: application/vnd.ms-excel");
		header("Content-Type: application/octet-stream");
		header("Content-Type: applicaiton/download");
		header('Content-Disposition:inine;filename="' . $outputFileName . '"');
		header("Content-Transfer-Encoding: binary");
		header("Pragma: no-cache");
		$objWriter->save('php://output');
		exit;
	}
	
	function actionExportSearch()
	{
		$this->_view['title'] = '导出（按搜索）';
		$product_info = Online_Product_Info::search(unserialize($_GET['url_arr']));
		if (count($product_info['data']) > 200)
		{
			return $this->_redirectMessage('导出数据的数量过多!', '正在返回..', $_SERVER['HTTP_REFERER']);
		}
		else
		{
			include Q::ini('custom_system/lib_dir') . 'PHPExcel.php';
			$phpexcel = new PHPExcel();
			$phpexcel->getActiveSheetIndex(0);
			$phpexcel->getActiveSheet()->setTitle('产品资料描述-按条件');
			$phpexcel->getActiveSheet()->getStyle("A1:D1")->getFont()->setBold(true);
			$phpexcel->getActiveSheet()->setCellValue('A1', 'SKU');
			$phpexcel->getActiveSheet()->setCellValue('B1', 'Language');
			$phpexcel->getActiveSheet()->getColumnDimension('C')->setWidth(120);
			$phpexcel->getActiveSheet()->setCellValue('C1', 'Title');
			$phpexcel->getActiveSheet()->getColumnDimension('D')->setWidth(120);
			$phpexcel->getActiveSheet()->setCellValue('D1', 'Description');
			$i = 2;
			foreach ($product_info['data'] as $pi)
			{
				$phpexcel->getActiveSheet()->setCellValueExplicit('A' . $i, $pi['number'], PHPExcel_Cell_DataType::TYPE_STRING);
				$phpexcel->getActiveSheet()->setCellValue('B' . $i, Helper_BSS_Normal::getIniOne('online_ebay_product_language', 'value', $pi['lang'], 'text'));
				$phpexcel->getActiveSheet()->setCellValue('C' . $i, $pi['title']);
				$phpexcel->getActiveSheet()->setCellValue('D' . $i, $pi['description']);
				$i++;
			}
			$outputFileName = '产品资料描述-按条件.xls';
			$objWriter = new PHPExcel_Writer_Excel5($phpexcel);
			header("Content-Type: application/vnd.ms-excel");
			header("Content-Type: application/octet-stream");
			header("Content-Type: applicaiton/download");
			header('Content-Disposition:inine;filename="' . $outputFileName . '"');
			header("Content-Transfer-Encoding: binary");
			header("Pragma: no-cache");
			$objWriter->save('php://output');
			exit;
		}
	}
}